/**
 * <p>多选的组织树控件</p>
 * xtype = cceafmultiselectgroup

 * @module Ext-ux-cceaf-control
 * @class cceaf.control.MultiSelectGroup
 * @extends Ext.Panel
 * @since 1.0
 */
Ext.define('cceaf.control.MultiSelectGroup',{
	extend: 'Ext.Panel', 
    alias: 'widget.cceafmultiselectgroup',

	constructor: function(config){
		config = config || {};
		
	    var groupTree = new Ext.tree.Panel({
	    	multiSelect: true,
	        //el:'tree-div',
	        autoScroll:true,
	        animate:true,
	        enableDD:true,
	        containerScroll: true,
	        border: false,
	        //width:150,
	        anchor:'100% -30',
	        
	        root: {
	            expanded: true,
	            text: cceaf.util.AjaxHelper.getGroupName('1'),//返回组织ID=1的组织名称，组织ID定为组织的根节点
		        draggable:false,
		        id:'1'
	        },
	        
	        store: new Ext.data.TreeStore({
	            proxy: {
	                type: 'ajax',
	                url: 'pageDataProvider!getGroupTreeList.action',
	                extraParams: {
	                	groupid: '1'
	                }
	            },
	            model: 'cceaf.model.main.CheckedTreeNodeModel'
	        }),
	        
	        viewConfig : {  
	            onCheckboxChange : function(e, t) {  
	                var item = e.getTarget(this.getItemSelector(), this.getTargetEl()), record;  
	                if (item) {  
	                    record = this.getRecord(item);  
	                    var check = !record.get('checked');  
	                    record.set('checked', check);  
	  
	                    if (check) {  
	                        record.bubble(function(parentNode) {  
                               	//parentNode.set('checked', true);  
                            });  
	                        record.cascadeBy(function(node) {  
                                node.set('checked', true);  
	                        });  
	                    } else {  
	                        record.cascadeBy(function(node) {  
                            	node.set('checked', false);  
                            });  
	                    }  
	                }  
	            }  
	        }
	    });
    
	    //groupTree.setRootNode(groupRoot);

		config = Ext.applyIf(config || {}, {
	        layout:'anchor',
	        //anchorSize: {width:200, height:600},
	        autoScroll:true,
	        items:[groupTree],
	        buttons: [
               	{xtype: 'button',text: ' 确  定 ',pressed: true,width: 70,
               		handler: function(){
               			var multiSelectGroup = this.findParentByType('cceafmultiselectgroup');
               			var fillGridTree = Ext.getCmp(multiSelectGroup.fillGridId);
						
						var multiSelectGroupTree = multiSelectGroup.items.getAt(0);
               			var checkedRecords = multiSelectGroupTree.getChecked();
               			if (checkedRecords.length == 0) {
							cceaf.util.ExtHelper.showInfoDialog('请选择组织机构！');
							return;
						}
						
               			//提交选择的数据
               			multiSelectGroup.submitData(checkedRecords);
               			
				    	var multiSelectGroupWin = multiSelectGroup.findParentByType('window');
				    	if (multiSelectGroupWin) {
				    		multiSelectGroupWin.close();
				    	}
              		}
               },
               
               {xtype: 'button',text: ' 关  闭 ',pressed: true,width: 70,
               		handler: function(){
               			var multiSelectGroup = this.findParentByType('cceafmultiselectgroup');
	              		var multiSelectGroupWin = multiSelectGroup.findParentByType('window');
				    	if(multiSelectGroupWin){
				    		multiSelectGroupWin.close();
				    	}
               		}
               }
	        ]
		});

		cceaf.control.MultiSelectGroup.superclass.constructor.apply(this, arguments);
		
		//展开根节点
		//groupRoot.expand();
	},
	
	/**
	* 判断store中是否存在同样id的组织
	* 
	* @param {Ext.data.Store} store 组织stroe
	* @param {Number} fieldName 待填充的组织ID字段名称
	* @param {Number} id 选择的组织ID
	* @return {Boolean} 存在返回true
	*/
	isExistSame: function(store, fieldName, id){
		var count = store.getCount();
		for	(i=0;i<count;i++) {
			record = store.getAt(i);
			if (record.get(fieldName) == id) {
				return true;
			}
		}
		return false;
	},
	
	/**
	* 选择后需要执行该方法把选中的数据填充，子类需要重载该方法
	* 
	* @param {Ext.data.Model[]} selectedItem 选中的组织
	*/
	submitData: function(selectedItem){
		cceaf.util.ExtHelper.showInfoDialog('请重载cceafmutilselectgroup的submitData方法');
	}
	
});